package activity.setting;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;
import activity.CustomView.CropImageView;
import base.HiActivity;
import bean.MyCamera;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;
import custom.SwitchButton;
import utils.HiToast;
/**
 * 报警移动侦测Activity
 * @author lt
 */
public class MotionDetectExtActivity extends HiActivity implements ICameraIOSessionCallback, View.OnClickListener {

    private MyCamera mCamera;
    HiChipDefines.HI_P2P_S_MD_PARAM md_param = null;
    HiChipDefines.HI_P2P_S_MD_PARAM md_param2 = null;
    HiChipDefines.HI_P2P_S_MD_PARAM md_param3 = null;
    HiChipDefines.HI_P2P_S_MD_PARAM md_param4 = null;
    private SwitchButton togbtn_motion_detection;
    @BindView(R.id.rg_area) RadioGroup rg_area;
    @BindView(R.id.rbtn_left_area) RadioButton rbtn_left_area;
    @BindView(R.id.rbtn_all_area) RadioButton rbtn_all_area;
    @BindView(R.id.rbtn_right_area) RadioButton rbtn_right_area;
    @BindView(R.id.cropimageview) CropImageView cropImageView;
    @BindView(R.id.ll_motion_area) LinearLayout ll_motion_area;
    @BindView(R.id.sb_sensitivity) SeekBar sb_sensitivity;
    @BindView(R.id.tv_sensitivity_rate) TextView tv_sensitivity_rate;
    @BindView(R.id.but_application) Button but_application;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_motion_detect_ext;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        String uid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        for (MyCamera camera : HiDataValue.CameraList) {
            if (uid.equals(camera.getUid())) {
                mCamera = camera;
                // 手机用的都是第一个区域,必须放在前面
                HiChipDefines.HI_P2P_S_MD_PARAM mdparam = new HiChipDefines.HI_P2P_S_MD_PARAM(0, new HiChipDefines.HI_P2P_S_MD_AREA(HiChipDefines.HI_P2P_MOTION_AREA_1, 0, 0, 0, 0, 0, 0));
                mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_MD_PARAM, mdparam.parseContent());
                HiChipDefines.HI_P2P_S_MD_PARAM mdparam2 = new HiChipDefines.HI_P2P_S_MD_PARAM(0, new HiChipDefines.HI_P2P_S_MD_AREA(HiChipDefines.HI_P2P_MOTION_AREA_2, 0, 0, 0, 0, 0, 0));
                mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_MD_PARAM, mdparam2.parseContent());
                HiChipDefines.HI_P2P_S_MD_PARAM mdparam3 = new HiChipDefines.HI_P2P_S_MD_PARAM(0, new HiChipDefines.HI_P2P_S_MD_AREA(HiChipDefines.HI_P2P_MOTION_AREA_3, 0, 0, 0, 0, 0, 0));
                mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_MD_PARAM, mdparam3.parseContent());
                HiChipDefines.HI_P2P_S_MD_PARAM mdparam4 = new HiChipDefines.HI_P2P_S_MD_PARAM(0, new HiChipDefines.HI_P2P_S_MD_AREA(HiChipDefines.HI_P2P_MOTION_AREA_4, 0, 0, 0, 0, 0, 0));
                mCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_MD_PARAM, mdparam4.parseContent());
                break;
            }
        }
        initView();
        setListeners();
    }

    private void setListeners() {
        rg_area.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rbtn_left_area:
                        cropImageView.setArea(1);
                        break;
                    case R.id.rbtn_all_area:
                        cropImageView.setArea(2);
                        break;
                    case R.id.rbtn_right_area:
                        cropImageView.setArea(3);
                        break;
                }
            }
        });
        sb_sensitivity.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(progress>=1&&progress<=100){
                    tv_sensitivity_rate.setText(progress+"");
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        but_application.setOnClickListener(this);
    }


    private void initView() {
        TitleView title = (TitleView) findViewById(R.id.title_top);
        title.setTitle(getResources().getString(R.string.title_alarm_motion_detection));
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        MotionDetectExtActivity.this.finish();
                        break;
                    case TitleView.NAVIGATION_BUTTON_RIGHT:
                        break;

                }

            }
        });
        cropImageView.getMainSteam(mCamera.u32Resolution);
        togbtn_motion_detection = (SwitchButton) findViewById(R.id.togbtn_motion_detection);
        sb_sensitivity.setMax(100);
    }

    @Override
    public void receiveIOCtrlData(HiCamera arg0, int arg1, byte[] arg2, int arg3) {
        if (arg0 != mCamera) return;

        Bundle bundle = new Bundle();
        bundle.putByteArray(HiDataValue.EXTRAS_KEY_DATA, arg2);
        Message msg = handler.obtainMessage();
        msg.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        msg.obj = arg0;
        msg.arg1 = arg1;
        msg.arg2 = arg3;
        msg.setData(bundle);
        handler.sendMessage(msg);

    }
    //发送命令
    protected void sendMotionDetection() {
        if (md_param == null && md_param2 == null && md_param3 == null && md_param4 == null) {
            return;
        }
        int guard_switch = togbtn_motion_detection.isChecked() ? 1 : 0;
        md_param.struArea.u32Enable = guard_switch;
        int md = Integer.parseInt(tv_sensitivity_rate.getText().toString());
        md_param.struArea.u32Sensi = md;
        md_param.struArea.u32Width= cropImageView.getmDrawableFloatWidth();
        md_param.struArea.u32Height= cropImageView.getmDrawableFloatHeight();
        md_param.struArea.u32Y= cropImageView.getmDrawableFloatY();
        md_param.struArea.u32X= cropImageView.getmDrawableFloatX();
        showjuHuaDialog();
        mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_MD_PARAM_NEW, md_param.parseContent());
        if (!togbtn_motion_detection.isChecked() && md_param2 != null) {
            md_param2.struArea.u32Enable = 0;
            mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_MD_PARAM_NEW, md_param2.parseContent());
        }
        if (!togbtn_motion_detection.isChecked() && md_param3 != null) {
            md_param3.struArea.u32Enable = 0;
            mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_MD_PARAM_NEW, md_param3.parseContent());
        }
        if (!togbtn_motion_detection.isChecked() && md_param4 != null) {
            md_param4.struArea.u32Enable = 0;
            mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_MD_PARAM_NEW, md_param4.parseContent());
        }

    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL: {
                    if (msg.arg2 == 0) {
                        Bundle bundle = msg.getData();
                        byte[] data = bundle.getByteArray(HiDataValue.EXTRAS_KEY_DATA);
                        switch (msg.arg1) {
                            case HiChipDefines.HI_P2P_GET_MD_PARAM:
                                HiChipDefines.HI_P2P_S_MD_PARAM md_param_temp = new HiChipDefines.HI_P2P_S_MD_PARAM(data);
                                if (md_param_temp.struArea.u32Area == HiChipDefines.HI_P2P_MOTION_AREA_1) {
                                    md_param = md_param_temp;
                                    togbtn_motion_detection.setChecked(md_param.struArea.u32Enable == 1 ? true : false);
                                    int sensitivity = md_param.struArea.u32Sensi;
                                    if(sensitivity>=1&&sensitivity<=100){
                                        sb_sensitivity.setProgress(sensitivity);
                                        tv_sensitivity_rate.setText(sensitivity+"");
                                    }
                                    handSensiArea();

                                } else if (md_param_temp.struArea.u32Area == HiChipDefines.HI_P2P_MOTION_AREA_2) {
                                    md_param2 = md_param_temp;
                                } else if (md_param_temp.struArea.u32Area == HiChipDefines.HI_P2P_MOTION_AREA_3) {
                                    md_param3 = md_param_temp;
                                } else if (md_param_temp.struArea.u32Area == HiChipDefines.HI_P2P_MOTION_AREA_4) {
                                    md_param4 = md_param_temp;
                                }
                                break;
                            case HiChipDefines.HI_P2P_SET_MD_PARAM_NEW:
                                HiToast.showToast(MotionDetectExtActivity.this,"应用成功");
                                dismissjuHuaDialog();
                                break;

                        }
                    } else {
                        switch (msg.arg1) {
                            case HiChipDefines.HI_P2P_SET_MD_PARAM_NEW:
                                HiToast.showToast(MotionDetectExtActivity.this, "应用失败!");
                                break;
                        }
                    }
                }
            }
        }
    };

    private void handSensiArea() {
        if(md_param!=null){
            int X= md_param.struArea.u32X;
            int Y=md_param.struArea.u32Y;
            int width=md_param.struArea.u32Width;
            int height=md_param.struArea.u32Height;
            if(mCamera.snapshot!=null){
                cropImageView.setDrawable(mCamera.snapshot,width,height,X,Y);
            }else {
                Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.videoclip);
                cropImageView.setDrawable(bitmap,width,height,X,Y);
            }

        }
    }

    @Override
    public void receiveSessionState(HiCamera arg0, int arg1) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCamera != null) {
            mCamera.registerIOSessionListener(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mCamera != null) {
            mCamera.unregisterIOSessionListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_application://应用
                sendMotionDetection();
                break;
        }
    }
}
